package com.example.maybeaccount.Spinner;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.example.maybeaccount.R;

import java.util.List;

public class CommonSpinner<T> extends RelativeLayout {
    private Context context;//上下文
    private List<T> datas;//下拉菜单数据集
    private View Spinner;
    private SpinnerPopupWindow<T> popupWindow;
    private LinearLayout rl_text;
    private EditText et_text;
    private LinearLayout iv_text;

    private String showTitle;
    private String showText;

    //接口，消息响应
    private Spinner_ClickListener spinner_ClickListener;
    //用于显示popupWindow
    private View.OnClickListener clickListener= new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.common_iv:
                    popupWindow.setWidth(rl_text.getWidth());
                    popupWindow.showAsDropDown(rl_text);

                    //               setTextImage(R.mipmap.arrow_down_bl);
                    break;
            }
        }
    };
    //用于popupWindow中ListView的子项点击事件
    private AdapterView.OnItemClickListener itemClickListener=new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            popupWindow.dismiss();
            et_text.setText(datas.get(position).toString());
            showText = datas.get(position).toString();
            //这个接口是用来在其他界面做点击操作的时候负责调用的
            if(spinner_ClickListener != null){
                spinner_ClickListener.ClickListener(datas.get(position).toString());
            }
        }
    };

    public CommonSpinner(Context context, String showTitle, List<T> datas) {
        super(context);
        this.context=context;
        this.showTitle = showTitle;
        this.datas=datas;
        init();
    }
    //设置接口
    public void setSpinner_ClickListener(Spinner_ClickListener spinner_ClickListener){
        this.spinner_ClickListener = spinner_ClickListener;
    }
    private void init() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Spinner = inflater.inflate(R.layout.common_spinner_layout, this);
        rl_text=(LinearLayout) Spinner.findViewById(R.id.common_rl);
        et_text = (EditText) Spinner.findViewById(R.id.common_et);
        iv_text=(LinearLayout) Spinner.findViewById(R.id.common_iv);
        et_text.setText(showTitle);
        //设置TextView点击事件
        iv_text.setOnClickListener(clickListener);
        //初始化popupWindow
        popupWindow = new SpinnerPopupWindow<>(getContext(), datas, itemClickListener);
        popupWindow.setOnDismissListener(dismissListener);
    }

    public void changedatas(List<T> newdata){
        if(!newdata.get(0).toString().equals("自定义")){
            et_text.setText(newdata.get(0).toString());
        }else{
            et_text.setText("");
        }
        datas=newdata;
        popupWindow = new SpinnerPopupWindow<>(getContext(), newdata, itemClickListener);
        popupWindow.setOnDismissListener(dismissListener);
    }
    /**
     * 监听popupwindow取消
     */
    private PopupWindow.OnDismissListener dismissListener = new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            //setTextImage(R.mipmap.repeat_arrow_right);
        }
    };
    /**
     * 给TextView右边设置图片
     * @param resId
     */
    private void setTextImage(int resId) {
        Drawable drawable = getResources().getDrawable(resId);
//        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());// 必须设置图片大小，否则不显示
//        et_text.setCompoundDrawables(null, null, drawable, null);
        LinearLayout img =  findViewById(R.id.common_iv);
        img.setBackground(drawable);
    }

    //用于外部获取TextView中的值
    public String getText(){
        return et_text.getText().toString().trim();
    }
    /**
     * @Description: 在外部获取Spinner界面上的EditText
     * @Author:lgy
     * @Date:2019/5/20 15:22
     */
    public EditText getEditView(){
        return et_text;
    }

    public interface Spinner_ClickListener {
        public void ClickListener(String data);
    }
}
